//棋盘的大小
var WIDTH = 450;
var HEIGHT = 450;
//true 为黑子
var me = true;
//游戏是否结束
var over = false;
//棋盘
var chessBoard = [];
//赢法数组
var wins = [];
//赢法的计数
var count = 0;

//赢法的统计数组
myWin = [];
computerWin = [];

//初始化
for (let i = 0; i < 15; i++) {
    chessBoard [i] = [];
    wins[i] = [];
    for (let j = 0; j < 15; j++) {
        chessBoard [i][j] = 0;
        wins[i][j] = [];
    }
}

for (let i = 0; i < 15; i++) {
    for (let j = 0; j < 11; j++) {
        // 所有横线上的赢法
        // wins[0][0][0] = true
        // wins[0][1][0] = true
        // wins[0][2][0] = true
        // wins[0][3][0] = true
        // wins[0][4][0] = true

        // wins[0][1][1] = true
        // wins[0][2][1] = true
        // wins[0][3][1] = true
        // wins[0][4][1] = true
        // wins[0][5][1] = true
        for (let k = 0; k < 5; k++) {
            wins [i][j + k][count] = true;
        }
        count++;
    }
}
// 所有竖线上的赢法
for (let i = 0; i < 15; i++) {
    for (let j = 0; j < 11; j++) {
        for (let k = 0; k < 5; k++) {
            wins [j + k][i][count] = true;
        }
        count++;
    }
}
// 所有斜线上的赢法
for (let i = 0; i < 11; i++) {
    for (let j = 0; j < 11; j++) {
        for (let k = 0; k < 5; k++) {
            wins [j + k][i + k][count] = true;
        }
        count++;
    }
}
// 所有反斜线上的赢法
for (let i = 0; i < 11; i++) {
    for (let j = 14; j > 3; j--) {
        for (let k = 0; k < 5; k++) {
            wins [i + k][j - k][count] = true;
        }
        count++;
    }
}
console.log(count);

//初始化赢法统计数组
for (let i = 0; i < count; i++) {
    myWin[i] = 0;
    computerWin[i] = 0;
}


var canvas = document.getElementById('canvas');
canvas.width = WIDTH;
canvas.height = HEIGHT;
var context = canvas.getContext('2d');
context.strokeStyle = '#BFBFBF';

var logo = new Image();
logo.src = 'img/logo.png';

logo.onload = function () {
    //调整背景图片的透明度
    context.globalAlpha = 0.05;
    context.beginPath();
    context.drawImage(logo, 0, 0, WIDTH, HEIGHT,);
    context.closePath();
    context.save();
    //将透明度属性清空
    context.globalAlpha = 1.0;
    //绘制棋盘
    drawChessBoard();
    //oneStep(0, 0, true);
    //oneStep(1, 1, false);
}

var drawChessBoard = function () {
    for (let i = 0; i < 15; i++) {
        context.moveTo(15 + 30 * i, 15);
        context.lineTo(15 + 30 * i, 435);
        context.stroke();
        context.moveTo(15, 15 + 30 * i);
        context.lineTo(435, 15 + 30 * i);
        context.stroke();
    }
}

var oneStep = function (i, j, me) {
    context.beginPath();
    context.arc(15 + 30 * i, 15 + 30 * j, 13, 0, 2 * Math.PI);
    context.closePath();
    var gradient = context.createRadialGradient(15 + 30 * i + 2, 15 + 30 * j - 2, 13, 15 + 30 * i, 15 + 30 * j, 0);
    if (me) {
        gradient.addColorStop(0, '#0A0A0A');
        gradient.addColorStop(1, '#636766');
    } else {
        gradient.addColorStop(0, '#D1D1D1');
        gradient.addColorStop(1, '#F9F9F9');
    }

    context.fillStyle = gradient;
    context.fill();
}

canvas.onclick = function (e) {
    if (over) {
        return;
    }
    if (!me) {
        return;
    }
    var x = e.offsetX;
    var y = e.offsetY;
    var i = Math.floor(x / 30);
    var j = Math.floor(y / 30);
    if (chessBoard[i][j] == 0) {
        oneStep(i, j, me);
        chessBoard[i][j] = 1;
        for (let k = 0; k < count; k++) {

            if (wins[i][j][k]) {
                //将玩家的赢法统计值加一
                myWin[k]++;
                //将computer的赢法统计置为异常值
                computerWin[k] = 6;
                if (myWin[k] == 5) {
                    console.log(over)
                    window.alert('你赢了！');
                    over = true;
                }
            }
        }

        if (!over) {
            me = !me;
            computerAI();
        }
    }
}
//计算机AI的下棋算法
var computerAI = function () {

    // 分数
    var myScore = [];
    var computerScore = [];

    var max = 0;
    var u = 0, v = 0;

    // 初始化
    for (let i = 0; i < 15; i++) {
        myScore[i] = [];
        computerScore[i] = [];
        for (let j = 0; j < 15; j++) {
            myScore[i][j] = 0;
            computerScore[i][j] = 0;
        }
    }
    for (let i = 0; i < 15; i++) {
        for (let j = 0; j < 15; j++) {
            // 如果棋盘中的位置没有落子则计算机可以落子
            if (chessBoard[i][j] == 0) {
                // 计算分数
                for (let k = 0; k < count; k++) {
                    if (wins[i][j][k]) {
                        // 拦截
                        if (myWin[k] == 1) {
                            myScore[i][j] += 200;
                        } else if (myWin[k] == 2) {
                            myScore[i][j] += 400;
                        } else if (myWin[k] == 3) {
                            myScore[i][j] += 2000;
                        } else if (myWin[k] == 4) {
                            myScore[i][j] += 10000;
                        }
                        // 自己下子
                        if (computerWin[k] == 1) {
                            computerScore[i][j] += 220;
                        } else if (computerWin[k] == 2) {
                            computerScore[i][j] += 420;
                        } else if (computerWin[k] == 3) {
                            computerScore[i][j] += 2100;
                        } else if (computerWin[k] == 4) {
                            computerScore[i][j] += 20000;
                        }
                    }
                }
                if (myScore [i][j] > max) {
                    // 记录分数和坐标
                    max = myScore [i][j];
                    u = i;
                    v = j;
                } else if (myScore [i][j] == max) {
                    if (computerScore [i][j] > computerScore [u][v]) {
                        u = i;
                        v = j;
                    }
                }
                if (computerScore [i][j] > max) {
                    // 记录分数和坐标
                    max = computerScore [i][j];
                    u = i;
                    v = j;
                } else if (computerScore [i][j] == max) {
                    if (myScore [i][j] > myScore [u][v]) {
                        u = i;
                        v = j;
                    }
                }
            }
        }
    }
    console.log(u, v, max);
    oneStep(u, v, false);
    chessBoard[u][v] = 2;
    //修改赢法统计数组
    for (let k = 0; k < count; k++) {

        if (wins[u][v][k]) {
            //将computer的赢法统计置为异常值
            computerWin[k]++;
            //将玩家的赢法统计值加一
            myWin[k] = 6;
            if (computerWin[k] == 5) {
                window.alert('计算机赢了！');
                over = true;
            }
        }
    }
    //轮到玩家下子
    if (!over) {
        me = !me;
    }
}